<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>8.轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .carousel {
            margin-top: 50px;
            margin-left: 200px;
            width: 1000px;
            height: 437.5px;
            position: relative;
        }

        .cal-list {
            width: 1000px;
            height: 437.5px;
            position: relative;
            /* 超出不显示 */
            overflow: hidden;

        }

        .viewpage {
            position: absolute;
            left: 0;
            top: 0;
            width: 700%;
            height: 437.5px;
        }

        .itme {
            float: left;
        }

        .pic {
            width: 1000px;
            height: 437.5px;
        }

        .prev,
        .next {
            width: 30px;
            height: 50px;
            font-size: 25px;
            background-color: rgba(255, 255, 255, 0.5);
            position: absolute;
            top: 43%;
            color: #fff;
            text-align: center;
            border-radius: 0 25px 25px 0;
            display: none;
        }

        .next {
            right: 0;
            border-radius: 25px 0 0 25px;
        }

        .circle-list {
            padding: 10px;
            position: absolute;
            bottom: 10px;
            margin-left: -60px;
            width: 125px;
            background-color: rgba(255, 255, 255, 0.7);
            border-radius: 50px;
            left: 50%;
        }

        .circle {
            float: left;
            margin-left: 5px;
            width: 15px;
            height: 15px;
            background-color: pink;
            cursor: pointer;
            border-radius: 50%;
        }

        .active {
            background-color: orange;
        }
    </style>
</head>

<body>
    <!-- 轮播图 -->
    <div class="carousel">
        <!-- 所有图片,图片列表 -->
        <div class="cal-list">
            <ul class="viewpage">
                <li class="itme"><img src="./img/one.png" class="pic"></li>
                <li class="itme"><img src="./img/two.png" class="pic"></li>
                <li class="itme"><img src="./img/three.png" class="pic"></li>
                <li class="itme"><img src="./img/four.png" class="pic"></li>
                <li class="itme"><img src="./img/five.png" class="pic"></li>
                <li class="itme"><img src="./img/six.png" class="pic"></li>
            </ul>
            <div class="prev">&lt;</div>
            <div class="next">&gt;</div>
        </div>
        <!-- 小圆点 -->
        <div class="circle-list">
            <span class="circle "></span>
            <span class="circle"></span>
            <span class="circle active"></span>
            <span class="circle"></span>
            <span class="circle"></span>
            <span class="circle"></span>
        </div>
    </div>
</body>
<script src="./js/utils.js"></script>
<script>
    var prev = document.getElementsByClassName('prev')[0];
    var next = document.getElementsByClassName('next')[0];
    var viewpage = document.getElementsByClassName('viewpage')[0];
    var itme = document.getElementsByClassName('itme')
    var calList = document.getElementsByClassName('cal-list')[0]
    var circleList = document.getElementsByClassName('circle-list')[0]
    var circle = document.getElementsByClassName('circle')
    //对应图片的索引
    //实现轮播
    var index = 0;
    //焦点的索引
    var circleIndex = 0
    var interid2;
    function autoplay() {
        interid2 = setInterval(function () {
            index++;
            circleIndex++;
            if (index >= itme.length) {
                index = 0;
                viewpage.style.left = 0 + 'px'
            }
            if (circleIndex == circle.length) {
                circleIndex = 0;
            }
            //初始化颜色
            initColor(circle);
            //设置当前焦点的颜色
            //给当前元素添加设置类属性
            circle[circleIndex].className = 'circle active'
            var target = -1000 * index;
            moveAnimation(viewpage, target);
        }, 1000)
    }
    autoplay()
    //鼠标悬停,停止轮播
    calList.onmouseover = function (e) {
        clearInterval(interid2)
        prev.style.display = 'block'
        next.style.display = 'block'
    }
    //鼠标离开继续轮播
    calList.onmouseout = function (e) {
        autoplay()
        prev.style.display = 'none'
        next.style.display = 'none'
    }
    //点击左右按钮进行移动
    next.onclick = function (e) {
        index++;
        circleIndex++;
        if (circleIndex == circle.length) {
            circleIndex = 0;
        }
        if (index == itme.length) {
            index = 0;
            viewpage.style.left = 0 + 'px'
        }
        //初始化所有颜色
        initColor(circle)
        //当前对象设置焦点颜色
        circle[circleIndex].className = 'circle active'
        var target = -1000 * index
        moveAnimation(viewpage, target)
    }
    prev.onclick = function (e) {
        index--;
        circleIndex--;
        if (circleIndex < 0) {
            circleIndex = circle.length
        }
        if (index == 0) {
            //如果是第一张图片直接跳到最后一张
            index = itme.length
            viewpage.style.left = 0 + 'px'
        }
        //初始化所有颜色
        initColor(circle)
        //当前对象设置焦点颜色
        circle[circleIndex].className = 'circle active'
        var target = -1000 * index;
        moveAnimation(viewpage, target)
    }
    function initColor(arr) {
        for (var i = 0; i < arr.length; i++) {
            arr[i].className = 'circle'
        }
    }
    //循环添加焦点事件
    for (var i = 0; i < circle.length; i++) {
        circle[i].ind = i;
        circle[i].onclick = function (e) {
            index = this.ind;
            circleIndex = index;
            initColor(circle);
            circle[circleIndex].className = 'circle active';
            var target = -1000 * index;
            moveAnimation(viewpage, target);
        }
    }
</script>

</html>